package com.eleven.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eleven.dao.CommentsMapper;
import com.eleven.pojo.Comments;
import com.eleven.pojo.CommentsExample;
import com.eleven.pojo.Projects;
import com.eleven.pojo.User;
import com.eleven.service.CommentsService;
@Service
public class CommentsServiceImpl implements CommentsService{

	@Autowired
	CommentsMapper commentsMapper;

	public CommentsMapper getCommentsMapper() {
		return commentsMapper;
	}

	public void setCommentsMapper(CommentsMapper commentsMapper) {
		this.commentsMapper = commentsMapper;
	}

	/**
	 * 查看用户自己的评论
	 */
	public List<Comments> getCommentsByUserId(Integer userId) {
		CommentsExample example = new CommentsExample();
		example.createCriteria().andCmUsIdEqualTo(userId);
		return commentsMapper.selectByExampleWithPro(example);
	}

	/**
	 * 查看项目的评论
	 */
	public List<Comments> getCommentsByProId(Integer proId) {
		CommentsExample example = new CommentsExample();
		example.createCriteria().andCmPsIdEqualTo(proId);
		return commentsMapper.selectByExampleWithUser(example);
	}

	/**
	 * 查看所有评论
	 */
	public List<Comments> getAllComments() {
		return commentsMapper.selectAll();
	}

	/**
	 * 删除评论
	 */
	public int deleteComment(Integer cmId) {
		return commentsMapper.deleteByPrimaryKey(cmId);
	}

	/**
	 * 新增评论
	 */
	public int saveComment(Comments comments) {
		
		return commentsMapper.insertSelective(comments);
	}

	/**
	 * 查看评论详细信息
	 */
	public Comments getDetailedComment(Integer cmId) {
		return commentsMapper.selectByPrimaryKeyWithProAndUserAndType(cmId);
	}

	/**
	 * 模糊查询-我的评论
	 */
	public List<Comments> getMyCommentsByCondition(String condition, Integer cmUsId) {
		Comments comments = new Comments();
		Projects projects = new Projects();
		comments.setCmUsId(cmUsId);
		comments.setCmContent("%"+condition+"%");
		projects.setPsName("%"+condition+"%");
		comments.setProjects(projects);
		return commentsMapper.selectMyCommentsByCondition(comments);
	}

	/**
	 * 模糊查询-所有项目评论
	 */
	public List<Comments> getAllCommentsByCondition(String condition) {
		Comments comments = new Comments();
		Projects projects = new Projects();
		User user = new User();
		comments.setCmContent("%"+condition+"%");
		projects.setPsName("%"+condition+"%");
		user.setUsName("%"+condition+"%");
		comments.setProjects(projects);
		comments.setUser(user);
		return commentsMapper.selectAllCommentsByCondition(comments);
	}

	/**
	 * 模糊查询-某个项目评论
	 */
	public List<Comments> getProCommentsByCondition(String condition, Integer cmPsId) {
		Comments comments = new Comments();
		User user = new User();
		comments.setCmPsId(cmPsId);
		comments.setCmContent("%"+condition+"%");
		user.setUsName("%"+condition+"%");
		comments.setUser(user);
		
		return commentsMapper.selectProCommentsByCondition(comments);
	}
	
}
